//
//  MyGameButton.m
//  ButtonCocos2D
//
//  Created by admin on 3/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyGameButton.h"

@implementation MyGameButton
@synthesize pressed, touchHash;
@synthesize upSpriteFrame, downSpriteFrame, name;

///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////HELPER CLASS///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
float PI = 3.14159;

float degreesToRadians(float d){
	return d * (PI/180);
}
float radiansToDegrees(float r){
	return r * (180/PI);
}
float vectorToRadians(CGPoint vector){
	if(vector.y == 0){ vector.y = 0.000001f; }
	float baseRadians = atan(vector.x/vector.y);
	if(vector.y < 0){ baseRadians += PI; }	//Adjust for -Y
	return baseRadians;
}
CGPoint radiansToVector(float radians){
	return ccp(sin(radians-PI/2), cos(radians-PI/2));
}
float distanceBetweenPoints(CGPoint p1, CGPoint p2){
	return sqrt( pow( (p1.x-p2.x) ,2) + pow( (p1.y-p2.y) ,2) );
}
bool pointIsInRect(CGPoint p, CGRect r){
	bool isInRect = false;
	if( p.x < r.origin.x + r.size.width && 
	   p.x > r.origin.x &&
	   p.y < r.origin.y + r.size.height &&
	   p.y > r.origin.y )
	{
		isInRect = true;
	}
	return isInRect;
}
bool pointIsInCircle(CGPoint p, CGPoint origin, float radius){
	bool isInCircle = false;
	if(distanceBetweenPoints(p, origin) <= radius){
		isInCircle = true;
	}
	return isInCircle;
}
float angleDifference(float angleA, float angleB){
	float diff = fabs(angleA-angleB);
	if(fabs((angleA+360)-angleB) < diff){
		diff = fabs((angleA+360)-angleB);
	}
	if(fabs((angleA-360)-angleB) < diff){
		diff = fabs((angleA-360)-angleB);
	}
	if(fabs(angleA-(angleB+360)) < diff){
		diff = fabs(angleA-(angleB+360));
	}
	if(fabs(angleA-(angleB-360)) < diff){
		diff = fabs(angleA-(angleB-360));
	}
	return diff;
}
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////HELPER CLASS///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

-(id)init {
    self = [super init];
    if (self != nil) {
        
    }
    return self;
}

-(void)dealloc {
    [super dealloc];
}

- (void)processTouch:(CGPoint)point {
	CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
	[self setDisplayFrame:[cache spriteFrameByName:downSpriteFrame]];
	pressed = true;
	[self setColor:ccc3(255,200,200)];
}

- (void)processRelease {
	CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
	[self setDisplayFrame:[cache spriteFrameByName:upSpriteFrame]];
	pressed = false;
	[self setColor:ccc3(255,255,255)];
}
- (bool)checkTouchWithPoint:(CGPoint)point {
	if(pointIsInRect(point, [self rect])){
		return YES;
	}else{
		return NO;
	}
}

- (CGRect) rect {
	//We set our scale mod to make sprite easier to press.
	//This also lets us press 2 sprites with 1 touch if they are sufficiently close.
	float scaleMod = 1.5f;
	float w = [self contentSize].width * [self scale] * scaleMod;
	float h = [self contentSize].height * [self scale] * scaleMod;
	CGPoint point = CGPointMake([self position].x - (w/2), [self position].y - (h/2));
	
	return CGRectMake(point.x, point.y, w, h); 
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];
	point = [[CCDirector sharedDirector] convertToGL: point];
    
	//We use circle collision for our buttons
	if(pointIsInCircle(point, self.position, self.rect.size.width/2)){		
		touchHash = [touch hash];
		[self processTouch:point];
	}
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];
	point = [[CCDirector sharedDirector] convertToGL: point];
	
	if(pointIsInCircle(point, self.position, self.rect.size.width/2)){
		if(touchHash == [touch hash]){		//If we moved on this sprite
			[self processTouch:point];
		}else if(!pressed){					//If a new touch moves onto this sprite
			touchHash = [touch hash];
			[self processTouch:point];
		}
	}else if(touchHash == [touch hash]){	//If we moved off of this sprite
		[self processRelease];
	}
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];	
	CGPoint point = [touch locationInView: [touch view]];
	point = [[CCDirector sharedDirector] convertToGL: point];
	
	if(touchHash == [touch hash]){	//If the touch which pressed this sprite ended we release
		[self processRelease];
	}
}


@end
